package taskTracker;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;

import taskTracker.util.Serializable;

public abstract class TaggableObject implements Serializable {
	/** Tags associates with this object */
	private Vector tags = new Vector();

	public Vector getTags() {
		return tags;
	}

	/**
	 * Set tags of this object from an array of string
	 * 
	 * @param tags
	 */
	public void setTags(String[] tags) {
		// add tags, may create them (if required)
		Vector tempTags = new Vector();
		for (int i = 0; i < tags.length; i++) {
			Tag tag = Tag.getTag(tags[i]);
			tag.addTaggedObject(this);
			if (!tempTags.contains(tag)) {
				tempTags.addElement(tag);
			}
		}

		for (int i = 0; i < this.tags.size(); i++) {
			Tag temp = (Tag) this.tags.elementAt(i);
			if (!tempTags.contains(temp)) {
				temp.removeTaggedObject(this);
			}
		}

		this.tags = tempTags;
	}

	/**
	 * Set tags of this object from a vector of tag strings
	 * 
	 * @param tags
	 */
	public void setTags(Vector tags) {
		String stringOfTags[] = new String[tags.size()];
		for (int i = 0; i < tags.size(); i++) {
			stringOfTags[i] = new String((String) tags.elementAt(i));
		}
		setTags(stringOfTags);
	}

	/**
	 * Set tags from a string of tags joined with {@link Tag#TAG_DELIMITER}
	 * 
	 * @param tags
	 */
	public void setTags(String tagsString) {
		Vector tags = new Vector();
		int i;
		while (tagsString.length() > 0
				&& (i = tagsString.indexOf(Tag.TAG_DELIMITER)) != -1) {
			tags.addElement(tagsString.substring(0, i).trim());
			tagsString = tagsString.substring(i + 1);
		}
		if (tagsString.length() > 0)
			tags.addElement(tagsString.trim());
		setTags(tags);
	}

	/**
	 * Returns tags joined with delimiter
	 * 
	 * @return
	 */
	public String getTagsAsString() {
		String tags = "";
		for (int i = 0; i < this.tags.size(); i++) {
			if (i > 0)
				tags += Tag.TAG_DELIMITER;
			tags += this.tags.elementAt(i);
		}
		return tags;
	}

	public void deserialize(DataInputStream dis) {
		try {
			//read the tags number
			int tagNumber = dis.readInt();
			int tagIndex;
			Vector tagsVector = new Vector();
			for(int i=0;i<tagNumber;i++)
			{
				//read the tag index
				tagIndex = dis.readInt();
				//Get the tag with the given index
				Tag tag = (Tag)(Tag.getTagsVector().elementAt(tagIndex));
				tagsVector.addElement(tag.getName());	
				//add the tag to the tags vector
				tags.addElement(tag);
			}
			setTags(tagsVector);
			
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	
	public void serialize(DataOutputStream dos) {
		try {
			//write the size of the tags vector
			dos.writeInt(tags.size());
			String tagName;
			
			//write the indexes of the tags in the tags vector
			for (int i =0 ; i<tags.size();i++)
			{
				tagName= ((Tag)(tags.elementAt(i))).getName();
				dos.writeInt( ((Integer)(Tag.getTagsIndexes().get(tagName))).intValue() );
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
